package mhl.service;

import mhl.DAO.diningTableDao;
import mhl.domain.diningTable;

import java.util.*;

public class diningTableService {
    private diningTableDao dtd = new diningTableDao();
    public List<diningTable> listDiningtable(){
        String sql = "select id,status from diningTable";
        return dtd.getMany(sql, diningTable.class);

    }

    public diningTable singleDiningTable(int id) {
        String sql = "select * from diningTable where id = ?";
        return dtd.getSingle(sql, diningTable.class, id);
    }

    public int  orderDiningTable(String orderName,String orderTel,int id){
        String sql = "update diningTable set status = '已预定',orderName = ? ,orderTel = ? where id = ?";
        return dtd.updateTable(sql, orderName, orderTel, id);

    }



    public boolean isOrdered(int id){
        String sql = "select status from diningTable where id = ?";
        return "已预定".equals((String)dtd.getObject(sql, id));

    }

    public int unOrderDiningTable(int id){
        String sql = "update diningTable set status = '空',orderName = '' ,orderTel = '' where id = ?";
        return dtd.updateTable(sql, id);

    }

    public int  updateDiningTableStatus(int id){
        return  dtd.updateTable("update diningTable set status = '就餐中' where id = ?",id);
    }

    public boolean updateDiningTableStatusToNull(int id){
        return dtd.updateTable("update diningTable set status = '空',orderName = '',orderTel = '' where id = ?",id) > 0;
    }

    public boolean isOrdered(String name,String tel,int diningTableId){
        String sql  = "select * from diningTable where id = ?";
        diningTable dt = dtd.getSingle(sql, diningTable.class, diningTableId);
        return name.equals(dt.getOrderName()) && tel.equals(dt.getOrderTel());

    }


}
